<template>
    <!--  登录表单容器-->
    <div class="login_container">
      <!--登录区域-->
      <div class="login_box">
        <!--头像-->
        <div class="avatar_box">
          <img src="../assets/img/icon.gif">
        </div>
        <div class="login_form">
          <!--表单-->
        <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="demo-loginForm">
          <el-form-item label="" prop="username" >
            <el-input v-model="loginForm.username" prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input v-model="loginForm.password" prefix-icon="el-icon-lock"></el-input>
          </el-form-item>
          <el-form-item class="login_btn">
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
        </div>
      </div>

    </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        loginForm: {
          username: '',
          password:'',
        },
        loginRules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 6, max: 10, message: '长度在 6到 10个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        var that = this
        var i
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.get('http://localhost:8181/user/findAll').then(function (response) {
                console.log(response.data)
                console.log(that.loginForm.password)

                if (that.loginForm.password == response.data[0].password&&that.loginForm.username == response.data[0].username){
                 that.$router.push('/studentGrade')
                }else {
                  alert('用户名或密码错误')
                }



            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="less" scoped>
 .login_container{
   height: 100%;
   background-color: aquamarine;
 }
  .login_box{
    width: 450px;
    height: 380px;
    background-color: #FFFFFF;
    border-radius: 3px;
    position: absolute;//绝对位置
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%); //居中
  }
  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #EEEEEE;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 2px 12px 0 rosybrown;//阴影
    margin: -65px auto;
    background-color: #FFFFFF;

    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #EEEEEE;
    }
  }

  .login_form{
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0 20px;//上下为0，左右为20px
    box-sizing: border-box;//组件在盒子内
  }

  .login_btn{
    display: flex;
    justify-content: flex-end;//右对齐  flex-start（默认值）：左对齐
  }

</style>
